package com.ronghui.cash.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * 编码与解码操作工具类
 * 
 * @author huangyong
 * @since 1.0
 */
public class CodecUtil {

	private static Logger logger = LoggerFactory.getLogger(CodecUtil.class);

	/**
	 * 将 URL 编码
	 */
	public static String encodeURL(String str) {
		String target;
		try {
			target = URLEncoder.encode(str, "UTF-8");
		} catch (Exception e) {
			logger.error("编码出错！", e);
			throw new RuntimeException(e);
		}
		return target;
	}

	/**
	 * 将 URL 解码
	 */
	public static String decodeURL(String str) {
		String target;
		try {
			target = URLDecoder.decode(str, "UTF-8");
		} catch (Exception e) {
			logger.error("解码出错！", e);
			throw new RuntimeException(e);
		}
		return target;
	}

	/**
	 * 将字符串 Base64 编码
	 */
	public static String encodeBASE64(String str) {
		String target;
		try {
			target = Base64.encodeBase64URLSafeString(str.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			logger.error("编码出错！", e);
			throw new RuntimeException(e);
		}
		return target;
	}

	/**
	 * 将字符串 Base64 解码
	 */
	public static String decodeBASE64(String str) {
		String target;
		try {
			target = new String(Base64.decodeBase64(str), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			logger.error("解码出错！", e);
			throw new RuntimeException(e);
		}
		return target;
	}

	/**
	 * 将字符串 MD5 加密
	 */
	public static String encryptMD5(String str) {
		return DigestUtils.md5Hex(str);
	}

	/**
	 * 将密码字符串 MD5 加密
	 */
	public static String encryptPwdMD5(String str) {

		// 盐
		String salt = "(*&(^_^)&*)^&*^*&JKBuihkjt*(&(UIHJHGU&ydtr^^*&(UIHJHGU&fyt6984!!J7";
		str = salt + str;

		return DigestUtils.md5Hex(str);
	}

	/**
	 * 将字符串 SHA 加密
	 */
	public static String encryptSHA(String str) {
		return DigestUtils.sha1Hex(str);
	}

	/**
	 * 创建随机数
	 */
	public static String createRandom(int count) {
		return RandomStringUtils.randomNumeric(count);
	}

	/**
	 * 创建邀请码随机数
	 */
	public static String createInvitationRandom(int count) {
		return RandomStringUtils.random(count, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
	}

	/**
	 * 获取 UUID（32位）
	 */
	public static String createUUID() {
		return UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
	}

	/**
	 * 
	 * @return
	 */
	public static String createOrderNo() {
		return getCurrTimeAndRandom();
	}

	/**
	 * 获得n位随机数
	 */
	public static String getRandCode(int n) {
		Random random = new Random();
		String result = "";
		for (int i = 0; i < n; i++) {
			result += random.nextInt(10);
		}
		return result;
	}

	/**
	 * 当前时间加6位随即数
	 * 
	 */
	public static String getCurrTimeAndRandom() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmsss");
		String currTimeAndRandom = sdf.format(date) + getRandCode(9);
		return currTimeAndRandom;
	}
}
